<template>
    <dzs-dialog :title="title" :isShow="isShow" @close="onCancel" @open="onOpen">
        <dzs-form :loadingOption="formInfo" :status="status" ref="myForm" v-model:value="formData"
            :options="formOptions" @onCancel="onCancel" @onSubmit="onSubmit">
        </dzs-form>
    </dzs-dialog>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import type { dzsFormProps } from "@/components/dzs-form/@types";
import type { dzsFormInstance } from "@/components/dzs-form";
import type { formData } from "../@types";
import { ElNotification } from 'element-plus';
import {getPermissionList , addUserInfo , updateUserInfo} from "../api/api"
import type { tableItem } from "../@types";
const emit = defineEmits(['cancel', 'update:isShow', 'onHoldSearch'])
const props = defineProps<{
    dialogData?: tableItem,
    status: string,
    isShow: boolean
}>()

const myForm = ref<dzsFormInstance>();
const formData = ref<formData>({
    ID: "",
    user_name: "",
    role_ID: "",
    is_disable: false,
    email: "",
    password : ""
})

const formInfo = ref({
    loading: false,
})

const permissionList = ref<dzsFormProps['children'][]>([])

const title = computed(() => {
    return props.status === "add" ? "添加用户" : props.status === "edit" ? "编辑用户" : "查看用户详情"
})
const formOptions = computed<dzsFormProps['formOptions']>(() => {
    return {
        formProps: {
            "labelWidth": "100px"
        },
        formItem: [
            {
                label: "ID",
                type: "input",
                key: "ID",
                isHidden: true,
            },
            {
                label: "用户名称",
                type: "input",
                key: "user_name",
                props: {
                    placeholder: "请输入用户名称",
                },
                rules: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' }
                ]
            },
            {
                label: "邮箱",
                type: "input",
                key: "email",
                props: {
                    placeholder: "请输入邮箱",
                },
                rules: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                ]
            },
            {
                label: "密码",
                type: "input",
                key: "password",
                props: {
                    placeholder: "请输入密码",
                    type: "password",
                    showPassword: true
                },
            },
            {
                label: "权限",
                type: "select",
                key: "role_ID",
                props: {
                    placeholder: "请选择权限",
                },
                children: permissionList.value
            },
            {
                label: "是否禁用",
                type: "switch",
                key: "is_disable",
                props: {
                    placeholder: "请选择是否禁用",
                    activeText: "禁用",
                    inactiveText: "启用",
                    activeColor: "#13ce66",
                    inactiveColor: "#ff4949",
                    'active-value' : "1",
                    'inactive-value' : "0"
                },
            }
        ]
    }
})

onMounted(() => {
    onGetPermissionList();
})

const onCancel = () => {
    myForm.value?.onCancel<formData>(false, {
        success: (res) => {
            formData.value = res
        }
    })
    emit('cancel');
    emit('update:isShow', false);
}


const onOpen = () => {
    if (props.dialogData?.ID) {
        formData.value = props.dialogData
    }
}

const onGetPermissionList = () => {
    getPermissionList().then(res => {
        permissionList.value = res.data.data;
    })
}


const onSubmit = () => {
    formInfo.value.loading = true;
    if (props.status === "edit") {
        updateUserInfo({
            ...formData.value
        }).then(res => {
            formInfo.value.loading = false;
            ElNotification({
                title: '成功',
                message: "提交成功",
                type: 'success'
            })
            emit("onHoldSearch");
            onCancel();
        }).catch(()=>{
            formInfo.value.loading = false;
        })
        return;
    }
    addUserInfo({
        ...formData.value
    }).then(res => {
        formInfo.value.loading = false;
        ElNotification({
            title: '成功',
            message: "提交成功",
            type: 'success'
        })
        emit("onHoldSearch");
        onCancel();
    }).catch(()=>{
        formInfo.value.loading = false;
    })
}
</script>
<style scoped></style>